package com.dataextend.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisExhaustedPoolException;

@Slf4j
@Component
public class RedisClientModel {
    @Autowired
    @Qualifier("jedisPool")
    private JedisPool jedisPool;


    public void set(String key, String value, int expire) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.setex(key, expire, value);
        } catch (JedisExhaustedPoolException e1) {
            log.info(">>>set 1");
        } catch (Exception e) {
            log.info(">>>e:" + e);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
    }


    public String get(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.get(key);
        } catch (JedisExhaustedPoolException e1) {
            log.info(">>>get 1");
        } catch (Exception e) {
            log.info(">>>e:" + e);
        } finally {
            if (null != jedis) {
                jedis.close();
            }
        }
        return null;
    }

}
